package com.ymatou.iapi.systemnotice.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.common.util.Hash;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.systemnotice.parameter.NoticeDetailBean;
import com.ymatou.iapi.systemnotice.service.NoticeDetailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.OP_NoticeFile;
import com.ymttest.database.model.OP_NoticeInfo;
import com.ymttest.database.sqlwapper.YmtCsWapper;
/**
*查询通知明细
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_NoticeDetail{
	private static NoticeDetailBean noticedetailBean;
	private static NoticeDetailCall noticedetailCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询通知明细");
	}

	@Before
	public void caseUp() {
		noticedetailBean=new NoticeDetailBean();
		noticedetailCall=new NoticeDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(noticedetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NoticeDetail_001() {
		Logger.start(true,"查询通知明细成功");
		try {
			OP_NoticeInfo no = new YmtCsWapper().selectAllNoticeInfo().get(0);
			noticedetailBean.setNoticeId(no.getNoticeId());
			noticedetailCall.setData(noticedetailBean);
			noticedetailCall.callService();			
			Logger.verifyEquals("true", noticedetailCall.getString("Success"), "验证Success");
			JsonObject result = noticedetailCall.getJsonObject().get("Result").getAsJsonObject();
			String noticeId=result.get("NoticeId").getAsString();
			OP_NoticeInfo notice = new YmtCsWapper().selectNoticeInfoByNoticeid(noticeId).get(0);
			
			Logger.verifyEquals(notice.getTitle(), result.get("Title").getAsString(), "验证Title");
			Logger.verifyEquals(notice.getContent(), result.get("Content").getAsString(), "验证Content");
			
			JsonArray jslist = result.get("Files").getAsJsonArray();
			List<OP_NoticeFile> list = new YmtCsWapper().selectNoticeFileByNoticeid(noticeId);
			Logger.verifyEquals(jslist.size(), list.size(), "验证FileListSize");
			Map m=new HashMap<String,Integer>();
			for (JsonElement jsonElement : jslist) {
				m.put(jsonElement.getAsJsonObject().get("FilePath").getAsString(),jsonElement.getAsJsonObject().get("FileType").getAsInt());
			}
			for (OP_NoticeFile op_NoticeFile : list) {
				Logger.verifyNotNull(m.get(op_NoticeFile.getFilePath()),"验证filepath非空");
				Logger.verifyEquals(op_NoticeFile.getFileType(),m.get(op_NoticeFile.getFilePath()),"验证filepath非空");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_NoticeDetail_002() {
		Logger.start(true,"不输入noticid,默认查询出最近一条");
		try {
			noticedetailCall.setData(noticedetailBean);
			noticedetailCall.callService();			
			Logger.verifyEquals("true", noticedetailCall.getString("Success"), "验证Success");
			JsonObject result = noticedetailCall.getJsonObject().get("Result").getAsJsonObject();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NoticeDetail_003() {
		Logger.start(true,"错误的查询条件失败");
		try {
			noticedetailBean.setNoticeId("123");
			noticedetailCall.setData(noticedetailBean);
			noticedetailCall.callService();			
			Logger.verifyEquals("false", noticedetailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("5000", noticedetailCall.getString("ErrorCode"), "验证ErrorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}